//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vS_AjoutAdresse.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//                      CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TAjoutAdresse::TAjoutAdresse(TComponent* Owner, const int Logiciel, const int Version)
    : TForm(Owner)
{
    mPageWeb->Clear();

    m_nLogiciel = Logiciel;
    m_nVersion  = Version;

    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text =  (AnsiString)"select log_nom,"+     //0
                                                "ver_nom,"+     //1
                                                "ref_nom "+     //2
                                        "from refer "+
                                            "left outer join logiciels on "+
                                                "(refer.log_code = logiciels.log_code) "+
                                            "inner join versions on "+
                                                "(refer.ver_code = versions.ver_code) "+
                                        "where refer.log_code = "+m_nLogiciel+" and refer.ver_code = "+m_nVersion;
    MaRequete->Open();
    MaRequete->First();
    eLogiciel->Text = MaRequete->Fields->Fields[0]->AsString;
    eVersion->Text  = MaRequete->Fields->Fields[1]->AsString;
    
    while(!MaRequete->Eof) {

        if(!VerifierRef(MaRequete->Fields->Fields[2]->AsString))
            cbRef->Items->Add(MaRequete->Fields->Fields[2]->AsString);
        MaRequete->Next();
    }
    
    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}

//---------------------------------------------------------------------------
//              AJOUTER UNE REFERENCE
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::bbOkClick(TObject *Sender)
{
    int idx = cbRef->ItemIndex;
    int NbreRef = CompterReference(cbRef->Items->Strings[idx]);

    if( (!cbRef->Text.IsEmpty()) && (idx != 0) ) {
        if(VerifierRefAdresse(cbRef->Items->Strings[idx], mPageWeb->Text)) {
          //Insertion dans la table reference
          //---------------------------------
          //reference déjà existante
          //------------------------
          if( idx != -1  ) {
              if(NbreRef != 0) {
                  //Affichage de la fenêtre
                ModalResult = mrOk;
              }
          }else {
              dmWebHelp->tRefer->Active = true;
              dmWebHelp->tRefer->Insert();
              ntRefer(LOG_CODE)       = m_nLogiciel;
              ntRefer(VER_CODE)       = m_nVersion;
              astRefer(REF_NOM)       = cbRef->Text;
              astRefer(REF_SOUS_NOM)  = eDesignation->Text;
              astRefer(REF_ADRESSE)   = mPageWeb->Text;
              dmWebHelp->tRefer->Post();
              dmWebHelp->trWebHelp->Commit();

              ModalResult = mrOk;
          }

        }else {
            ModalResult = mrNone;
            MessageDlg("Cette référence et cette adresse sont déjà existante.", mtWarning, TMsgDlgButtons() << mbOK, 0);
        }
    }else {
        ModalResult = mrNone;
        MessageDlg("Veuillez sélectionner ou saisir une référence.", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }
}
//---------------------------------------------------------------------------
//              COMPTER LE NOMBRE DE REFERENCES
//---------------------------------------------------------------------------
int __fastcall TAjoutAdresse::CompterReference( const AnsiString Ref )
{
    int NombreDeRef = 0;
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text =  (AnsiString)"select count(ref_nom) "+
                                        "from refer "+
                                        "where (log_code = "+m_nLogiciel+" and ver_code= "+m_nVersion+") and ref_nom = \'"+Ref+"\'";
    MaRequete->Open();
    MaRequete->First();

    NombreDeRef = MaRequete->Fields->Fields[0]->AsInteger;

    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return NombreDeRef;
}
//---------------------------------------------------------------------------
//              EVENEMENT COMBO BOX REFERENCE
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::cbRefChange(TObject *Sender)
{
    int idx = cbRef->ItemIndex;
    if( (idx != 0)&&(idx != -1) ) {
        cbRef->Style = csDropDownList;
    }else {
        cbRef->Style = csDropDown;
    }
}
//---------------------------------------------------------------------------
//              VERIFICATION DE L ADRESSE ET DE LA REFERENCE
//---------------------------------------------------------------------------
bool __fastcall TAjoutAdresse::VerifierRefAdresse(    const AnsiString Ref,
                                                        const AnsiString Adresse )
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text =  (AnsiString)"select count(ref_nom) from refer "+
                                        "where (ref_adresse = \'"+Adresse+"\' and ref_nom = \'"+Ref+"\') "+
                                        "and (log_code = "+m_nLogiciel+" and ver_code = "+m_nVersion+")";
    MaRequete->Open();
    MaRequete->First();

    if(MaRequete->Fields->Fields[0]->AsInteger == 0)
        bRetour = true;

    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//              CONSULTER LA PAGE WEB
//---------------------------------------------------------------------------
void __fastcall TAjoutAdresse::bbConsulterClick(TObject *Sender)
{
    if(!mPageWeb->Text.IsEmpty())
        ShellExecute(NULL, "Open", mPageWeb->Text.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
    else {
        MessageDlg("Veuillez remplir le champ Page Web", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }
}
//---------------------------------------------------------------------------
//              EVITER LES DOUBLONS DANS LA COMBO BOX
//---------------------------------------------------------------------------
bool __fastcall TAjoutAdresse::VerifierRef(const AnsiString Ref)
{
    bool bRetour = false;
    int idx = cbRef->Items->Count;
    for( int i=0; i<idx; i++) {
        if(Ref == cbRef->Items->Strings[i]) {
            bRetour = true;
        }
    }
    return bRetour;
}
